<?PHP

include '../wikibot.classes.php';include 'ircbot.config.php';

$newdomain  = new domain;$wpapi  = new wikipediaapi;$wpq    = new wikipediaquery;$wpi    = new wikipediaindex;$wpapi->login($user,$pass);
$run = $wpq->getpage('User:'.$user.'/Run');
$getdomain = $newdomain->getdomain($domain);
$fp = fsockopen($irchost, $ircport, $erno, $errstr, 30);if (!$fp) {    echo $errstr." (".$errno.")<br />\n";} else {
	    fwrite($fp, "NICK ".$ircnick."\r\n");    fwrite($fp, "USER ".$ircident." ".$irchost." bla :".$ircrealname."\r\n");    fwrite($fp, "JOIN :".$ircchannel."\r\n");        fwrite($fp, "PRIVMSG ".$chan." :Hello There!\r\n");    fwrite($fp, "PRIVMSG ".$chan." :I am a bot\r\n");        // loop through each line to look for ping     while (!feof($fp)) {                 $line =  fgets($fp, 128);        echo $line."\n";                $line = explode(":ping ", $line);                echo $line[0]."\n";        if ($line[1]) {                        fwrite($fp, "PONG ".$line[1]."\r\n");         }    }        fclose($fp);}





$ircconfig = explode("\n",$wpq->getpage('User:'.$owner.'/CBChannels.js'));$tmp = array();foreach($ircconfig as $tmpline) { 
	if (substr($tmpline,0,1) != '#') { 
		$tmpline = explode('=',$tmpline,2); 
		$tmp[trim($tmpline[0])] = trim($tmpline[1]); 
	} 
}$ircchannel = $tmp['ircchannel'];unset($tmp,$tmpline);        $stalkbots = array();        $trialbots = explode("\n",$wpq->getpage('Wikipedia:Bots/Requests for approval'));        foreach ($trialbots as $trialbot) if (preg_match('/\{\{BRFA\|(.*)\|.*\|Trial\}\}/',str_replace(array("\n","\r"),'',$trialbot),$m)) $stalkbots[$m[1]] = 1;        $irc = fsockopen($ircserver,$ircport);        $ircpid = pcntl_fork();        if ($ircpid == 0) {                fwrite($irc,'PASS '.$ircpass."\n");                fwrite($irc,'USER '.$user.' "1" "1" :ClueBot Wikipedia Bot.'."\n");                fwrite($irc,'NICK '.$user."\n");                while (!feof($irc)) {                        $data = str_replace(array("\n","\r"),'',fgets($irc,1024));//                      echo 'IRC: '.$data."\n";                        $d = explode(' ',$data);                        if (strtolower($d[0]) == 'ping') {                                fwrite($irc,'PONG '.$d[1]."\n");                        } elseif (($d[1] == '376') or ($d[1] == '422')) {//                              fwrite($irc,'PRIVMSG NickServ :identify '.$pass."\n");//                              sleep(2);                                fwrite($irc,'JOIN '.$ircchannel.','.$ircdebugchannel.','.$ircreportchannel.','.$ircvandalismchannel.','.$ircaivchannel.','.$irctechchannel.','.$ircproxychannels.','.$ircunrevertedchannels.','.$ircbagtrialchannels.','.$ircotherchannels."\n");                                foreach (explode(',',$ircchannel) as $y) {                                        fwrite($irc,'PRIVMSG '.$y.' :IRC logging enabled.'."\n");                                }                        } elseif (strtolower($d[1]) == 'privmsg') {                                if (substr($d[3],0,2) == ':!') {                                        if (strtolower($d[2]) == '#wikipedia-en') { $tmp = explode('!',substr($d[0],1)); $cmd = 'NOTICE '.$tmp[0]; }                                        elseif (strtolower($d[2]) == strtolower($user)) { $tmp = explode('!',substr($d[0],1)); $cmd = 'NOTICE '.$tmp[0]; }                                        else { $cmd = 'PRIVMSG '.$d[2]; }                                        switch (substr(strtolower($d[3]),2)) {                                                case 'edit':                                                        if (preg_match("/\[\[(.*)\]\]/",$data,$m)) {                                                                $rv = $wpapi->revisions($m[1],1,'older');                                                                fwrite($irc,$cmd.' :[['.$m[1].']] http://en.wikipedia.org/w/index.php?title='.urlencode($m[1]).'&diff=prev' .                                                                        '&oldid='.urlencode($rv[0]['revid']).' * '.$rv[0]['user'].' * '.$rv[0]['comment']."\n");                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'stalk':                                                        if (preg_match("/\[\[User:(.*)\]\]/",$data,$m)) {                                                                $uc = $wpapi->usercontribs($m[1],1);                                                                fwrite($irc,$cmd.' :[['.$uc[0]['title'].']] http://en.wikipedia.org/w/index.php?title='.urlencode($uc[0]['title']).'&diff=prev' .                                                                        '&oldid='.urlencode($uc[0]['revid']).' * '.$m[1].' * '.$uc[0]['comment']."\n");                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'beaten':                                                        if (preg_match("/\[\[User:(.*)\]\]/",$data,$m)) {                                                                if (!mysql_ping($mysql)) { $mysql = mysql_pconnect($mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass); mysql_select_db($mysqldb, $mysql); }                                                                $x = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) AS `count` FROM `beaten` WHERE `user` = \''.mysql_real_escape_string($m[1]).'\' GROUP BY `user`'));                                                                $y = mysql_fetch_assoc(mysql_query('SELECT SQL_CALC_FOUND_ROWS COUNT(`id`) AS `count2` FROM `beaten` GROUP BY `user` HAVING `count2` > \''.mysql_real_escape_string($x['count']).'\' LIMIT 1'));                                                                $z = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() as `ahead`'));                                                                fwrite($irc,$cmd.' :[[User:'.$m[1].']] has beaten me '.(($x['count'] != '')?$x['count']:'0').' times.  There are '.$z['ahead'].' users who have beaten me more times.'."\n");                                                                unset($x,$y);                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'vandalcount':                                                        if (preg_match("/\[\[(.*)\]\]/",$data,$m)) {                                                                $tmp = unserialize(file_get_contents('oftenvandalized.txt'));                                                                if (isset($tmp[$m[1]])) {                                                                        fwrite($irc,$cmd.' :[['.$m[1].']] has been vandalized '.count($tmp[$m[1]]).' time(s) in the last 48 hours.'."\n");                                                                } else {                                                                        fwrite($irc,$cmd.' :[['.$m[1].']] has not been vandalized in the last 48 hours.'."\n");                                                                }                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'heuristics':                                                        include 'cluebot.heuristics.config.php';                                                        $stats = unserialize(file_get_contents('cluebot.heuristics.stats.txt'));                                                        fwrite($irc,$cmd.' :I have the following heuristics enabled: '.implode(', ',$heuristics).".\n");                                                        foreach ($stats as $heuristic => $count) {                                                                fwrite($irc,$cmd.' :The '.$heuristic.' heuristic has been matched '.$count.' times.'."\n");                                                        }                                                        unset($count,$heuristic,$stats,$heuristics);                                                        break;                                                case 'status':                                                        $ov = unserialize(file_get_contents('oftenvandalized.txt'));                                                        foreach ($ov as $title => $array) {                                                                if (count($array) == 0) unset($ov[$title]);                                                        }                                                        file_put_contents('oftenvandalized.txt',serialize($ov));                                                        $count = count($ov);                                                        $titles = unserialize(file_get_contents('titles.txt'));                                                        foreach ($titles as $title => $time) {                                                                if ((time() - $time) > (24*60*60)) {                                                                        unset($titles[$title]);                                                                }                                                        }                                                        file_put_contents('titles.txt',serialize($titles));                                                        $tcount = count($titles);                                                        foreach ($ov as $x => $y) {                                                                $ocount[$x] = count($y);                                                        }                                                        arsort($ocount);                                                        foreach ($ocount as $x => $y) {                                                                $mova = $x;                                                                $movacount = $y;                                                                break;                                                        }                                                        preg_match('/\(\'\'\'\[\[([^|]*)\|more...\]\]\'\'\'\)/iU',$wpq->getpage('Wikipedia:Today\'s featured article/'.date('F j, Y')),$tfa);                                                        $tfa = $tfa[1];                                                                                                                if (!preg_match('/(yes|enable|true)/i',$wpq->getpage('User:'.$user.'/Run'))) {                                                                $run = false;                                                        } else {                                                                $run = true;                                                        }                                                        $top5beat = array();                                                        if (!mysql_ping($mysql)) { $mysql = mysql_pconnect($mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass); mysql_select_db($mysqldb, $mysql); }                                                        $q = mysql_query('SELECT `user`,COUNT(`id`) AS `count` FROM `cluebot_enwiki`.`beaten` WHERE `user` != \'\' GROUP BY `user` HAVING `count` > 1 ORDER BY `count` DESC LIMIT 5');                                                        while ($x = mysql_fetch_assoc($q)) {                                                                $top5beat[] = $x['user'].' ('.$x['count'].')';                                                        }                                                        unset($x,$q);                                                        $top5beat = implode(' - ',$top5beat);                                                        fwrite($irc,$cmd.' :I am '.$user.'.  I am currently '.($run?'enabled':'disabled').'.  I currently have '.$wpq->contribcount($user).' contributions.'."\n");                                                        fwrite($irc,$cmd.' :I have attempted to revert '.$tcount.' unique article/user combinations in the last 24 hours.  ' .                                                                'I know of '.$count.' different articles that have been vandalized in the last 48 hours.'."\n"                                                                );                                                        fwrite($irc,$cmd.' :[['.$mova.']] is the most vandalized page with a total of '.$movacount.' vandalisms in the last 48 hours.  ' .                                                                'Today\'s featured article is: [['.$tfa.']].'."\n"                                                                );                                                        fwrite($irc,$cmd.' :The following users have beat me to the revert the most: '.$top5beat."\n");                                                        fwrite($irc,$cmd.' :I log all information to '.$ircchannel.'.  This channel is '.$d[2].'.'."\n");                                                        unset($x,$y,$count,$ov,$tcount,$ocount,$mova,$movacount,$tfa,$run,$title,$titles,$time,$top5beat);                                                        break;                                                case 'warninglevel':                                                        if (preg_match("/\[\[User:(.*)\]\]/",$data,$n)) {                                                                $warning = 0;                                                                if (preg_match_all('/<!-- Template:(uw-[a-z]*(\d)(im)?|Blatantvandal \(serious warning\)) -->.*(\d{2}):(\d{2}), (\d+) ([a-zA-Z]+) (\d{4}) \(UTC\)/iU',                                                                        $wpq->getpage('User talk:'.$n[1]),                                                                        $match,PREG_SET_ORDER)                                                                ) {                                                                        foreach ($match as $m) {                                                                                $month = array('January' => 1, 'February' => 2, 'March' => 3,                                                                                        'April' => 4, 'May' => 5, 'June' => 6, 'July' => 7,                                                                                        'August' => 8, 'September' => 9, 'October' => 10,                                                                                        'November' => 11, 'December' => 12                                                                                );                                                                                if ($m[1] == 'Blatantvandal (serious warning)') $m[2] = 4;                                                                                if ((time() - gmmktime($m[4],$m[5],0,$month[$m[7]],$m[6],$m[8])) <= (2*24*60*60)) {                                                                                        if ($m[2] > $warning) { $warning = $m[2]; }                                                                                }                                                                        }                                                                }                                                                fwrite($irc,$cmd.' :[[User:'.$n[1].']] is at warning level '.$warning.".\n");                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'count':                                                        if (preg_match("/\[\[User:(.*)\]\]/",$data,$n)) {                                                                fwrite($irc,$cmd.' :[[User:'.$n[1].']] has '.$wpq->contribcount($n[1])." contributions.\n");                                                        } else {                                                                fwrite($irc,$cmd.' :Couldn\'t find link.'."\n");                                                        }                                                        break;                                                case 'help':                                                        fwrite($irc,$cmd.' :Please see [[User:'.$user.']].'."\n");                                                        break;                                                case 'eval':                                                        $tmp = explode(' ',$data,6);                                                        $tmp1 = explode('!',substr($d[0],1));                                                        if ($d[4] == md5($thesecret.$tmp1[0].$tmp[5])) {                                                                eval($tmp[5]);                                                        } else {                                                                fwrite($irc,$cmd.' :Code incorrect.'."\n");                                                        }                                                        break;                                                case 'cbproxy':                                                        $tmp = explode(' ',$data,6);                                                        $tmp1 = explode('!',substr($d[0],1));                                                        if ($tmp1[0] == 'ClueBot-Bopm') {                                                                foreach (explode(',',$ircproxychannels) as $y) {                                                                        fwrite($irc,'PRIVMSG '.$y.' :!admin '.$tmp[5]."\n");                                                                }                                                                                                                                $data = $wpq->getpage('Wikipedia:WikiProject on open proxies');                                                                if (strpos($data,$tmp[4]) === false) {                                                                        $header = explode('{{proxyip2|127.0.0.1}} || Example',$data,2);                                                                        $header[0] .= '{{proxyip2|127.0.0.1}} || Example ';                                                                        $footer = $header[1];                                                                        $header = $header[0];                                                                        $data = "\n".'{{proxyip2|'.$tmp[4].'}} || '.$tmp[5].' ~~~~'."\n";                                                                        $data = $header.$data.$footer;                                                                        unset($header,$footer);                                                                        $wpi->post('Wikipedia:WikiProject on open proxies',$data,'Adding '.$tmp[4].'.');                                                                        unset($data);                                                                }                                                        }                                                        break;                                        }                                }                        }                }                die();        }